// pages/xzmd/xzmd.js
var api = require('../../utils/api.js')
const config = require(`../../config.js`);
Page({

  /**
   * 页面的初始数据
   */
  data: {

    personal: [], //个人
    business: [], //商家
    empower: [], //授权
    type: '',
    shopid: '',
    authUserId: '',
    shopIds:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    let type = wx.getStorageSync("type")
    let shopIds = wx.getStorageSync("userMess").shopId
    // let shopIds ='956a9ef22e1c411085f39a68c859b831'
   
    let personal = [] //个人
    let business = [] //商家
    let empower = [] //授权
    wx.showLoading({
      title: '加载中',
    })
    wx.login({
      success: res => {
        api.switchUser({
          wxCode: res.code
        }).then(res => {
          if (res.result) {
            wx.hideLoading()
            res.object.forEach(function(v) {
              if (v.type == 1) {
                business.push(v)
              } else if (v.type == 2) {
                personal.push(v)
              } else if (v.type == 0) {
                empower.push(v)
              }
            })
            that.setData({
              business: business,
              personal: personal,
              empower: empower,
              type: type,
              shopIds,
            })
            if (type == 1) {
              business.forEach(function (v) {
                if (v.shopId == shopIds) {
                  that.setData({
                    type: v.type,
                    shopid: v.shopId,
                    authUserId: v.id,
                  })
                }
              })
            } else if (type == 2) {
              personal.forEach(function (v) {
                if (v.shopId == shopIds) {
                  that.setData({
                    type: v.type,
                    shopid: v.shopId,
                    authUserId: v.id,
                  })
                }
              })
            } else if (type == 0) {
              empower.forEach(function (v) {
                if (v.shopId == shopIds) {
                  that.setData({
                    type: v.type,
                    shopid: v.shopId,
                    authUserId: v.id,
                  })
                }
              })
            }
          }
        })
      }
    })
  },


  //个人
  personal: function() {
    wx.navigateTo({
      url: '../edit-address/edit-address?newAdd='+1,
    })
  },
  //商家注册
  business: function() {
    wx.navigateTo({
      url: '../type/type?newAdd='+1,
    })
  },


  userType: function(e) {
    
    this.setData({
      type: e.currentTarget.dataset.type,
      shopid: e.currentTarget.dataset.shopid,
      shopIds: e.currentTarget.dataset.shopid,
      authUserId: e.currentTarget.dataset.authuserid,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  //我的页面
  yjmx: async function() {
    
    let that = this
    
    wx.login({
      success: res => {
        wx.request({
          url: `${config.url}/admin/user/login`,
          data: {
            wxCode: res.code,
            authUserId: that.data.authUserId,
            type: that.data.type,
            shopId: that.data.shopid
          },
          header: {
            'content-type': 'application/json',
          },
          method: 'POST',
          success: function(res) {
            if (res.data.result) {

              // 当前用户状态 商家 个人
              wx.setStorageSync("type", res.data.object.type)
              //本地存储
              wx.setStorageSync("token", res.data.data)
              //codeNum
              wx.setStorageSync("codeNum", res.data.code)
              wx.request({
                url: `${config.url}/admin/user/getUserInfo`,
                method: 'POST',
                data: {
                  phone: wx.getStorageSync("phone"),
                  cityId: wx.getStorageSync("cityId"),
                },
                header: {
                  'content-type': 'application/json',
                  //每次从本地获取toekn传给后台
                  'jtxs': wx.getStorageSync("token")
                },
                success:async function (res) {
                  if (res.data.result) {
                    if( res.data.data == "0"){
                      wx.setStorageSync('authFunction', res.data.body.authUserType)
                    }else{
                      wx.removeStorageSync('authFunction')
                    }
                    wx.setStorageSync("storeName", res.data.body.name)
                    wx.setStorageSync("codeNum", res.data.body.code)
                    wx.setStorageSync("type", res.data.data)
                    wx.setStorageSync("vipFlag", res.data.object.vipFlag)
                    wx.setStorageSync("openId", res.data.object.openid)
                    
                    // 清空购物车
                    const response = await api.emptyShoppingCart({})
                    if(response.result) {
                      wx.reLaunch({
                        url: '../my/my'
                      });
                    }
                  }
                }
              })
          
            } else {
              wx.showToast({
                title: '网络出现问题',
                icon: 'none'
              })
            }

          }
        })
      }
    })

  }
})